// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CLOUDAPPENGINE_CAEPIPEMANAGER_H
#define CLOUDAPPENGINE_CAEPIPEMANAGER_H

#include <pthread.h>
#include <queue>
#include "../cae_common/CaeDefine.h"
#include "../cae_common/CaeLogConfig.h"
#include "CaePipeData.h"

class CaePipeManager {
public:
    static CaePipeManager &GetInstance();
    ~CaePipeManager();

    std::queue<CaePipeData *> m_videoQueue;
    std::queue<CaePipeData *> m_audioQueue;
    std::queue<CaePipeData *> m_cmdQueue;
    std::queue<CaePipeData *> m_channelQueue;
    std::queue<CaePipeData *> m_imeQueue;
    std::queue<CaePipeData *> m_virtualDeviceQueue;
    std::queue<CaePipeData *> m_screenshotQueue;

    int ReadSize(uint8_t type) const;

    CaePipeData *ReadOnePkt(uint8_t type);

    static void AppendData(uint8_t *data, size_t size, bool addMsgHeader, uint8_t type);

private:
    CaePipeManager(const CaePipeManager &) = delete;
    CaePipeManager &operator = (const CaePipeManager &) = delete;
    CaePipeManager();

    pthread_mutex_t m_videoQueueMutex;
    pthread_mutex_t m_audioQueueMutex;
    pthread_mutex_t m_cmdQueueMutex;
    pthread_mutex_t m_channelQueueMutex;
    pthread_mutex_t m_imeQueueMutex;
    pthread_mutex_t m_virtualDeviceQueueMutex;
    pthread_mutex_t m_screenshotQueueMutex;
    void WriteFully(uint8_t *buff, uint32_t size, uint8_t type);
};

#endif // CLOUDAPPENGINE_CAEPIPEMANAGER_H
